namespace SeataNet.Core.Transaction
{
    /// <summary>
    /// 资源管理器接口
    /// </summary>
    public interface IResourceManager
    {
        /// <summary>
        /// 注册分支事务
        /// </summary>
        /// <param name="branchType">分支事务类型</param>
        /// <param name="xid">全局事务ID</param>
        /// <param name="resourceId">资源ID</param>
        /// <param name="lockKey">锁键</param>
        /// <param name="applicationData">应用数据</param>
        /// <returns>分支事务ID</returns>
        Task<long> BranchRegisterAsync(string branchType, string xid, string resourceId, string? lockKey = null, string? applicationData = null);
        
        /// <summary>
        /// 报告分支事务状态
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <param name="branchId">分支事务ID</param>
        /// <param name="status">事务状态</param>
        /// <param name="applicationData">应用数据</param>
        /// <returns>报告结果</returns>
        Task<bool> BranchReportAsync(string xid, long branchId, int status, string? applicationData = null);
        
        /// <summary>
        /// 锁查询
        /// </summary>
        /// <param name="xid">全局事务ID</param>
        /// <param name="lockKeys">锁键集合</param>
        /// <returns>是否可以锁定</returns>
        Task<bool> LockQueryAsync(string xid, ICollection<string> lockKeys);
        
        /// <summary>
        /// 提交分支事务
        /// </summary>
        /// <param name="branchType">分支事务类型</param>
        /// <param name="xid">全局事务ID</param>
        /// <param name="branchId">分支事务ID</param>
        /// <param name="resourceId">资源ID</param>
        /// <param name="applicationData">应用数据</param>
        /// <returns>提交结果</returns>
        Task<bool> BranchCommitAsync(string branchType, string xid, long branchId, string resourceId, string? applicationData = null);
        
        /// <summary>
        /// 回滚分支事务
        /// </summary>
        /// <param name="branchType">分支事务类型</param>
        /// <param name="xid">全局事务ID</param>
        /// <param name="branchId">分支事务ID</param>
        /// <param name="resourceId">资源ID</param>
        /// <param name="applicationData">应用数据</param>
        /// <returns>回滚结果</returns>
        Task<bool> BranchRollbackAsync(string branchType, string xid, long branchId, string resourceId, string? applicationData = null);
        
        /// <summary>
        /// 注册资源
        /// </summary>
        /// <param name="resource">资源</param>
        void RegisterResource(IResource resource);
        
        /// <summary>
        /// 注销资源
        /// </summary>
        /// <param name="resource">资源</param>
        void UnregisterResource(IResource resource);
        
        /// <summary>
        /// 获取所有资源
        /// </summary>
        /// <returns>资源集合</returns>
        ICollection<IResource> GetAllResources();
    }
}